package com.drought.rained.ui.activity.repayment;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.R;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.bean.JsOrderDataLJ;
import com.drought.rained.bean.RolloveDataLJ;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.mvpview.RolloveMvpViewLJ;
import com.drought.rained.network.UrlsLJ;
import com.drought.rained.presenter.RollovePresenterLJ;
import com.drought.rained.response.RolloveResponseLJ;
import com.drought.rained.ui.activity.WebViewActivityLJ;
import com.drought.rained.utils.StringUtilsLJ;
import com.drought.rained.utils.ToastUtilsLJ;
;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@SuppressLint("CheckResult")
@Route(name = "展期", path = RouterMapLJ.ROLLOVER_PAGE)
public class RolloveActivityLJ extends BaseTitleActivity implements View.OnClickListener, RolloveMvpViewLJ {

    @BindView(R.id.tv_rollover_deadline)
    TextView tvRolloverDeadline;
    @BindView(R.id.tv_interest)
    TextView tvInterest;
    @BindView(R.id.tv_member_management_fee)
    TextView tvMemberManagementFee;
    @BindView(R.id.tv_account_maintenance_fee)
    TextView tvAccountMaintenanceFee;
    @BindView(R.id.layout_overdue_days)
    RelativeLayout overdueDaysLayout;
    @BindView(R.id.tv_overdue_days)
    TextView tvOverdueDays;
    @BindView(R.id.layout_overdue_fee)
    RelativeLayout overdueFeeLayout;
    @BindView(R.id.tv_overdue_fee)
    TextView tvOverdueFee;
    @BindView(R.id.tv_rollover_fee)
    TextView tvRolloverFee;
    @BindView(R.id.layout_check_agreement)
    LinearLayout checkAgreementLayout;
    @BindView(R.id.btn_refresh)
    Button btnRefresh;

    private String mOrderId;
    private String productTerm;
    private int mOrderType;
    private RolloveDataLJ mRolloveData;
    private RollovePresenterLJ rollovePresenter;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {
        mOrderId = getIntent().getStringExtra(ConstantLJ.ORDER_ID);
        productTerm = getIntent().getStringExtra(ConstantLJ.ORDER_PRODUCT_TERM);
        mOrderType = getIntent().getIntExtra(ConstantLJ.ORDER_LIST_TYPE, ConstantLJ.ORDER_LIST_TYPE_ROLLOVER);
        if (mOrderType == ConstantLJ.ORDER_STATUS_OVERDUE) { // 逾期展期
            overdueDaysLayout.setVisibility(View.VISIBLE);
            overdueFeeLayout.setVisibility(View.VISIBLE);
        }
        rollovePresenter = new RollovePresenterLJ(this);
        getApplyData();
    }

    @Override
    protected int getContentResId() {
        return R.layout.layout_of_rollove;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.rollover_page_tile);
    }

    @OnClick({R.id.layout_check_agreement, R.id.btn_refresh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_check_agreement:
                jumpToWebPage();
                break;
            case R.id.btn_refresh:
                if (mRolloveData == null || StringUtilsLJ.isEmpty(mRolloveData.getOrderId())
                        || StringUtilsLJ.isEmpty(mRolloveData.getTotalFee())) {
                    getApplyData();
                    return;
                }
                ARouter.getInstance()
                        .build(RouterMapLJ.REPAY_METHOD_PAGE)
                        .withString(ConstantLJ.ORDER_ID, mRolloveData.getOrderId())
                        .withString(ConstantLJ.ORDER_MONEY_NUM, mRolloveData.getTotalFee())
                        .withBoolean(ConstantLJ.ORDER_MONEY_IS_ROLLOVER, true)
                        .navigation();
                break;
        }
    }

    private void jumpToWebPage() {
        if (mRolloveData == null || StringUtilsLJ.isEmpty(mRolloveData.getOrderId())) {
            getApplyData();
            return;
        }

        JsOrderDataLJ orderData = new JsOrderDataLJ();
        orderData.setBorrowMoney(mRolloveData.getTotalFee());
        orderData.setBorrowDay(productTerm);
        orderData.setToken(UserInfoLJ.getToken());
        ARouter.getInstance()
                .build(RouterMapLJ.WEB_VIEW_PAGE)
                .withString(WebViewActivityLJ.PARAMS_URL, UrlsLJ.REGISTER_CONTRACT_URL)
                .withSerializable(WebViewActivityLJ.PARAMS_ORDER_DATA, orderData)
                .navigation();
    }

    public void getApplyData() {
        showLoadingView();
        rollovePresenter.getApplyData(mOrderId);
    }

    @SuppressLint("SetTextI18n")
    private void fillDisplayView(RolloveDataLJ data) {
        mRolloveData = data;
        String repaymentBrowingUnit = getResources().getString(R.string.bill_browing_unit);
        String moneyUnitHint = getResources().getString(R.string.money_unit_hint);
        String daysUnit = getResources().getString(R.string.bill_days_unit);
        tvRolloverDeadline.setText(productTerm + daysUnit);
        tvInterest.setText(data.getInterest() + repaymentBrowingUnit);
        tvMemberManagementFee.setText(data.getpCreditserviceFee() + repaymentBrowingUnit);
        tvAccountMaintenanceFee.setText(data.getpAccountFee() + repaymentBrowingUnit);
        if (mOrderType == ConstantLJ.ORDER_STATUS_OVERDUE) {
            tvOverdueDays.setText(data.getOverdueDay() + daysUnit);
            tvOverdueFee.setText(StringUtilsLJ.getDotMoneyStr(data.getOverdueFee()) + " " + moneyUnitHint);
        }
        tvRolloverFee.setText(StringUtilsLJ.getDotMoneyStr(data.getTotalFee()) + " " + moneyUnitHint);
    }

    @Override
    public void onGetDataNext(RolloveResponseLJ orderDetailResponse) {
        dismissLoadingView();
        if (orderDetailResponse.getStatus() == ConstantLJ.HTTP_OK) {
            fillDisplayView(orderDetailResponse.getData());
        } else {
            ToastUtilsLJ.showShort(RolloveActivityLJ.this, orderDetailResponse.getMessage());
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }
}
